package com.shop.shoporder.wapController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.UserLoginToken;
import com.shop.shoporder.entity.order.*;
import com.shop.shoporder.service.OrderAddressService;
import com.shop.shoporder.service.OrderListService;
import com.shop.shoporder.service.OrderService;
import com.shop.shoporder.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/wap")
@CrossOrigin(origins = "*",maxAge = 3600)
public class OrderListController {
    @Autowired
    OrderListService orderListService;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderAddressService orderAddressService;
    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/getOrderList",method = RequestMethod.GET)
    //用户订单列表
    public Response getOrderList(@RequestParam Map<String,String> map, HttpServletRequest request) {
        List<CartList> cartLists=new ArrayList<>();
        PageInfo<CartList> cartPageInfo=new PageInfo<>(cartLists);
        int pages = 0;
        int limits = 10;
        if(map.get("page")!=null){
            pages=Integer.parseInt(map.get("page"));
        }
        if(map.get("limit")!=null){
            limits=Integer.parseInt(map.get("limit"));
        }
        try{
            PageHelper.startPage(pages, limits);
            cartLists=orderListService.selectOrderList(map,request);
            if(cartLists!=null&&cartLists.size()>0){
                for (CartList cartList: cartLists) {
                    try {
                        int addressId=cartList.getAddressId()==null?0:cartList.getAddressId();
                        OrderAddress orderAddress=orderAddressService.selectByPrimaryKey(addressId);
                        if(orderAddress!=null) {
                            orderAddress.setProvinceId(null);
                            orderAddress.setCityId(null);
                            orderAddress.setAreaId(null);
                            orderAddress.setIsdefault(null);
                        }
                        cartList.setOrderAddress(orderAddress);
                    }catch (Exception r){}
                    Set<String> setorno = new HashSet();
                    for (Carts carts:cartList.getCartsList()) {
                        String logNo=carts.getLogisticalNo()==null?"":carts.getLogisticalNo();
                        if(!logNo.equals("")) {
                            for (String str : logNo.split(",")) {
                                if (str != null && !str.equals("")) {
                                    setorno.add(str);
                                }
                            }
                        }
                        carts.setLogisticalNo(null);
                    }
                    List<String> list1 = new ArrayList<>(setorno);
                    cartList.setLogisticsList(list1);
                }

                cartPageInfo=new PageInfo<>(cartLists);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
        if(cartLists!=null&&cartLists.size()>0){
            return new Response(MessageConstants.MSG_SUCCESS,cartPageInfo.getList(),cartPageInfo.getTotal(),"获取数据成功！",request);
        }else{
            return new Response(MessageConstants.MSG_SUCCESS,cartPageInfo.getList(),0,"获取数据失败！",request);
        }
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/getOrderDetails",method = RequestMethod.GET)
    //用户订单列表
    public Response getOrderDetails(@RequestParam Map<String,String> map, HttpServletRequest request) {
        SureOrder sureOrder=new SureOrder();
        try{
            sureOrder=orderListService.getOrderDetails(map,request);
            return new Response(MessageConstants.MSG_SUCCESS,sureOrder,1,"获取成功！",request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,sureOrder,0,"获取异常！",request);
        }
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/getOrderNumber",method = RequestMethod.GET)
    //用户订单列表
    public Response getOrderNumber(@RequestParam Map<String,String> map, HttpServletRequest request) {
        List<Order> list=new ArrayList<>();
        try{
            list=orderService.selectOrderNumber(map,request);
            return new Response(MessageConstants.MSG_SUCCESS,list,1,"获取成功！",request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,list,0,"获取异常！",request);
        }
    }

}
